<template>
	<view>
		<view class="sun-nav-title">
			<view class="sun-nav-content">
				<u-icon name="arrow-left" size="20" @click="bask"></u-icon>
				<image src="@/static/logo/i18n_icon.png" style="width: 16px;height: 19px;"></image>
			</view>
		</view>
		<view class="sun-index">
			<view style="position: relative;height: 280rpx;">
				<view style="display: flex;justify-content: center;">
					<view class="sun-logo-box" style="position: absolute;">
						<image class="sun-icon-img" src="@/static/logo/beijin3.png" />
					</view>
				</view>
			</view>
			<view style="position: relative;">
				<view class="sun-logo-denglu">
					<view class="sun-login-box">
						<view class="sun-login-text">
							<text>{{ t('register.name') }}</text>
						</view>

					</view>
					<view class="sun-login-box">
						<view class="sun-label">
							<text class="label-text">{{ t('login.mobile') }}</text>
						</view>
						<view class="sun-input-box">
							<input v-model="state.mobile" type="text" :placeholder="t('login.enterMobile')"
								placeholder-class="placeholder-class" />
							<!-- <image @click="state.mobile=''" class="close-icon" src="@/static/imgs/close_icon.png" /> -->
						</view>
					</view>
					<view class="sun-login-box">
						<view class="sun-label">
							<text class="label-text">{{ t('register.code') }}</text>
						</view>
						<view class="sun-input-box">
							<input v-model="state.verify_code" type="text" :placeholder="t('register.inputCode')"
								placeholder-class="placeholder-class" />
							<view>
								<u-code :seconds="seconds" @end="end" @start="start" ref="uCodeRef" @change="codeChange"
									:startText="t('register.codeText')"></u-code>
								<view @tap="getCode" class="sun-code-text">{{tips}}</view>
							</view>
						</view>
					</view>
					<view class="sun-login-box">
						<view class="sun-label">
							<text class="label-text">{{ t('login.password') }}</text>
						</view>
						<view class="sun-input-box">
							<u-input v-model="state.password" password border="none"
								:placeholder="t('login.enterPassword')" v-if="imageType">
								<template #suffix>
									<image src="../../static/imgs/yanjin1.png" style="width: 18px;height: 12px;"
										v-if="imageType" @click="tabImage"></image>
									<image src="../../static/imgs/yanjin2.png" style="width: 18px;height: 12px;"
										v-if="!imageType" @click="tabImage"></image>
								</template>
							</u-input>
							<u-input v-model="state.password" border="none" v-if="!imageType"
								:placeholder="t('login.enterPassword')">
								<template #suffix>
									<image src="../../static/imgs/yanjin1.png" style="width: 18px;height: 12px;"
										v-if="imageType" @click="tabImage"></image>
									<image src="../../static/imgs/yanjin2.png" style="width: 18px;height: 12px;"
										v-if="!imageType" @click="tabImage"></image>
								</template>
							</u-input>

							<!-- <input v-model="state.password" type="text" :placeholder="$t('login.enterPassword')"
								placeholder-class="placeholder-class" />
							<image @click="state.password=''" class="close-icon" src="@/static/imgs/close_icon.png" /> -->
						</view>
					</view>
					<view class="sun-login-box">
						<view class="sun-label">
							<text class="label-text">{{ t('register.rebuildPassword') }}</text>
						</view>
						<view class="sun-input-box">
							<u-input v-model="state.re_password" password border="none"
								:placeholder="t('login.enterPassword')" v-if="imageType2">
								<template #suffix>
									<image src="../../static/imgs/yanjin1.png" style="width: 18px;height: 12px;"
										v-if="imageType2" @click="tabImage2"></image>
									<image src="../../static/imgs/yanjin2.png" style="width: 18px;height: 12px;"
										v-if="!imageType2" @click="tabImage2"></image>
								</template>
							</u-input>
							<u-input v-model="state.re_password" border="none" v-if="!imageType2"
								:placeholder="t('login.enterPassword')">
								<template #suffix>
									<image src="../../static/imgs/yanjin1.png" style="width: 18px;height: 12px;"
										v-if="imageType2" @click="tabImage2"></image>
									<image src="../../static/imgs/yanjin2.png" style="width: 18px;height: 12px;"
										v-if="!imageType2" @click="tabImage2"></image>
								</template>
							</u-input>


						</view>
					</view>
					<view class="sun-login-box">
						<view class="sun-label">
							<text class="label-text">{{ t('register.invitationCode') }}</text>
						</view>
						<view class="sun-input-box">
							<input v-model="state.invite_code" type="text"
								:placeholder="t('register.enterinvitationCode')"
								placeholder-class="placeholder-class" />
							<!-- <image @click="state.invite_code=''" class="close-icon"
								src="@/static/imgs/close_icon.png" /> -->
						</view>
					</view>
					<view class="login-btn-box">
						<view class="login-btn" @click="handleSubmit">{{ t('register.next') }}</view>
					</view>
				</view>

			</view>
		</view>
		<view>
			<u-popup :show="pupbeijin" mode="center" :round="10" >
				<view style="padding: 20px;text-align: center;">
					<image src="../../static/imgs/logo_pupbeijin.png" style="width: 260px;height: 170px;"></image>
					<view style="color: rgba(102, 102, 102, 1);font-size: 16px;margin: 20px;">
						注册成功,请去登录
					</view> 
				</view>
			</u-popup>
		</view>
	</view>

</template>

<script setup>
	import {
		onMounted,
		reactive
	} from 'vue'
	import {
		useI18n
	} from "vue-i18n";
	const {
		t
	} = useI18n()
	import {
		ref
	} from 'vue';
	import {
		vMobile,
		smsSend,
		register
	} from '@/api/login/login.js'

	const state = reactive({
		mobile: '',
		password: '',
		re_password: '',
		verify_code: '',
		invite_code: ''
	})

	const imageType = ref(true)
	const imageType2 = ref(true)
	const checkValue = reactive([])
	let pupbeijin = ref(false)
	const tips = ref('');
	const seconds = ref(60);
	const uCodeRef = ref(null);

	const codeChange = (text) => {
		tips.value = text;
	};

	function handleSubmit() { 
		if (!state.mobile) return uni.showToast({
			title: t('login.enterMobile'),
			icon: 'none',
			duration: 1500
		})
		else if (!state.password) return uni.showToast({
			title: t('login.enterPassword'),
			icon: 'none',
			duration: 1500
		})
		else if (!state.re_password) return uni.showToast({
			title: '请输入重复密码',
			icon: 'none',
			duration: 1500
		})
		else if (!state.verify_code) return uni.showToast({
			title: '请输入验证码',
			icon: 'none',
			duration: 1500
		})
		else {
			register(state).then(res => {
				console.log(res)
				if (res.code == 200) {
					pupbeijin.value = true
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 2000)
				}
			})

		}


	}

	function tabImage() {
		imageType.value = !imageType.value
	}

	function tabImage2() {
		imageType2.value = !imageType2.value
	}
	const getCode = () => {
		uCodeRef.value.start();
	};

	const end = () => {
		// uni.$u.toast('倒计时结束');
	};

	const start = () => {
		let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
		console.log()
		if (state.mobile == '' || state.mobile == undefined) {
			uni.$u.toast('手机号不能为空,请输入手机号!');
		} else if (!reg.test(state.mobile)) {
			uni.$u.toast('请输入正确的手机号!');
		} else {
			vMobile({
				mobile: state.mobile
			}).then(res => {
				if (res.code == 200 && res.msg == '手机号有效') {
					smsSend({
						mobile: state.mobile,
						type: 1
					}).then(res => {
						uni.$u.toast('验证码已经发送成功!');
					})

				}
			})
		}

	};

	function bask() {
		uni.navigateBack()
	}
</script>

<style scoped>
	page {
		background: linear-gradient(180deg, rgba(239, 246, 255, 1) 0%, rgba(242, 244, 247, 1) 100%);
	}

	.sun-logo-box {
		display: flex;
		margin-top: 126rpx;
		justify-content: center;
		align-items: center;
	}

	.sun-logo {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 180rpx;
		height: 180rpx;
		border-radius: 15rpx;
		background-color: #12C8B9;
		box-shadow: 0rpx 0rpx 30rpx rgba(18, 200, 185, 0.5);
	}

	.sun-logo-denglu {
		background-color: #fff;
		margin: 40rpx;
		border-radius: 28rpx;
	}

	.close-icon {
		width: 36rpx;
		height: 34rpx;
	}

	.sun-icon-img {
		width: 700rpx;
		height: 500rpx;
	}

	.sun-login-box {
		padding: 20rpx 60rpx;
	}

	.sun-login-text {
		font-size: 48rpx;
		font-weight: 700;
		text-align: center;
	}

	.sun-label {
		display: flex;
		align-items: center;
	}

	.label-text {
		margin-left: 16rpx;
		font-weight: 500;
		color: #272e2d;
		font-size: 30rpx;
	}

	.sun-input-box {
		display: flex;
		align-items: center;
		height: 100rpx;
		border-bottom: 1rpx solid #eee;
		padding: 0px 10rpx;
	}

	.sun-input-box input {
		flex: 1;
	}

	.placeholder-class {
		font-size: 30rpx;
		color: #C0C0C0;
	}

	.sun-tip {
		display: flex;
		justify-content: space-between;
		padding: 0rpx 68rpx;
	}

	.sun-tip-text {
		color: rgba(18, 39, 167, 1);
		font-size: 13px;
	}

	.login-btn-box {
		width: 670rpx;
		padding: 50rpx 60rpx;
	}

	.login-btn-agr {
		position: absolute;
		bottom: 129rpx;
		width: 750rpx;
		padding: 50rpx 68rpx 0rpx;
		display: flex;
		justify-content: center;
	}

	.login-btn {
		height: 82rpx;
		border-radius: 41rpx;
		background: linear-gradient(90deg, rgba(77, 110, 223, 1) 0%, rgba(19, 40, 169, 1) 100%);
		box-shadow: -1px 12px 11px 0px rgba(77, 110, 223, 0.4);
		text-align: center;
		line-height: 82rpx;
		font-size: 36rpx;
		font-weight: 500;
		color: #fff;
	}

	.sun-code-text {
		font-size: 14px;
		color: rgba(41, 63, 204, 1);
	}

	.sun-nav-title {
		height: 50px;
		display: flex;
		align-items: center;
		padding: 0 40rpx;

	}

	.sun-nav-content {
		display: flex;
		flex: 1;
		justify-content: space-between;
	}
</style>